package edu.kaist.cs442.server.locationalpush.push.c2dm;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Configurable;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

@Configurable
public class GoogleHelper {

    static final String PARAM_REGISTRATION_ID = "registration_id";
    static final String PARAM_COLLAPSE_KEY = "collapse_key";
    static final String UTF8 = "UTF-8";
    static final String auth_token = "DQAAAK4AAAAW9RsKm6X7oiMMrge8XHAJbzyzTDp9YeYeZtVtKGrQ3yPurvAJuzr5bAp4seb2FQkU5vjUbI6YPNOwMcLULBtxUwAqYD3nFMqPQPRbhyoRnwL_uCjt6LYAuKwZ_QWzeFjzoIYdwrGSgq97qJjwPXSCGqdMAuCop-x_cxmylx5jEce2DsDLyldiQIR4Vj3LawVWSNCaOuGAQuFoBvQKgfnXl8EXQeGUBzlcIRNVIXcuHQ";



//return status
	public static int sendMessage(String registrationId, MessageVO message) {



        StringBuilder postDataBuilder = new StringBuilder();
        byte[] postData;

        postDataBuilder.append(PARAM_REGISTRATION_ID).append("=").append(registrationId);
        postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=").append(message.getCollapseKey());
        try {
            for (Map.Entry<String, String> pair : message.getContentList().entrySet()) {
                postDataBuilder.append("&").append("data." + pair.getKey()).append("=").append(URLEncoder.encode(pair.getValue(), UTF8));
            }
            postData = postDataBuilder.toString().getBytes(UTF8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return -1;
        }


        // Hit the dm URL.
        URL url = null;
        try {
            url = new URL("https://android.apis.google.com/c2dm/send");
        } catch (MalformedURLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });

        HttpsURLConnection conn = null;
        try {
            conn = (HttpsURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            conn.setRequestProperty("Content-Length", Integer.toString(postData.length));
            conn.setRequestProperty("Authorization", "GoogleLogin auth=" + auth_token);

            OutputStream out = conn.getOutputStream();
            out.write(postData);
            out.close();

            int responseCode = conn.getResponseCode();
            InputStream response = conn.getInputStream();

            byte[] a = new byte[10000];
            while (response.read(a, 0, a.length) > 0) {
                System.out.print(new String(a));
            }

            return responseCode;
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return -1;
        }

    }
}
